package com.spring.boot.action;

import com.spring.boot.enums.LoginEnum;
import com.spring.boot.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Ysoul on 15/11/10.
 */

@RestController
@RequestMapping("/spring/boot")
public class SpringBootStart {

    private static Logger logger = LoggerFactory.getLogger(SpringBootStart.class);

    @Autowired
    private LoginService loginService;

    @ResponseBody
    @RequestMapping(value = "/login_check", method = RequestMethod.POST)
    public Map<String, String> loginIn(HttpServletRequest request,
                                       @RequestParam("username") String userName,
                                       @RequestParam("password") String password,
                                       @RequestParam("captcha") String captcha) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("code", LoginEnum.SUCCESS.getCode().toString());
        map.put("msg", LoginEnum.SUCCESS.getMsg());
        LoginEnum ret = loginService.login(request, userName, password, captcha);
        if (LoginEnum.SUCCESS != ret) {
            map.put("code", ret.getCode().toString());
            map.put("msg", ret.getMsg());
        }
        return map;
    }
}
